package com.geyue.controller;

import com.geyue.common.toolbox.result.Res;
import com.geyue.common.util.StrUtils;
import com.geyue.core.model.Teacher;
import com.geyue.core.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by Lyon on 2017/4/24.
 */
@Controller
@RequestMapping("/admin/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 跳转登录页面
     * @return
     */
    @RequestMapping(value = "login",method = RequestMethod.GET)
    public String login(){
        return "login";
    }

    /**
     * 登录方法
     * @param session
     * @param tel
     * @param password
     * @return
     */
    @RequestMapping(value = "login",method = RequestMethod.POST)
    @ResponseBody
    public Res login(HttpSession session, String tel, String password){
        if(StrUtils.isEmpty(tel) || StrUtils.isEmpty(password))
            return Res.fail("用户名或密码不能为空!");

        Teacher teacher = new Teacher();
        teacher.setTel(tel);
        teacher.setPassword(password);
        teacher = teacherService.getOne(teacher);
        if(teacher == null)
            return Res.fail("用户名或密码不正确!");

        session.setAttribute("teacher",teacher);
        return Res.success("登录成功!");
    }

    /**
     * 注册页面
     * @return
     */
    @RequestMapping(value = "reg",method = RequestMethod.GET)
    public String reg(){
        return "reg";
    }

    /**
     * 注册方法
     * @param teacher
     * @return
     */
    @RequestMapping(value = "reg",method = RequestMethod.POST)
    @ResponseBody
    public Res reg(@Validated Teacher teacher){

        return Res.success("登录成功!");
    }


}
